package com.codewsq;

import com.codewsq.exer.User;
import org.junit.Test;

import java.util.*;

public class ArrayListTest {

    @Test
    public void test() {
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "zhangsan"));
        userList.add(new User(2, "lisi"));
        userList.add(new User(3, "wangwu"));
        userList.add(new User(4, "zhaoliu"));
        userList.add(new User(5, "tom"));

        for (int i = 0; i < userList.size(); i++) {
            if (i == 2) {
                User user = userList.get(i);
                user.setName("jerry");
            }
        }

        Iterator<User> iterator = userList.iterator();
        while (iterator.hasNext()) {
            User user = iterator.next();
            if (user.getId() == 4) {
                user.setName("park");
            } else {
                iterator.remove();
            }
        }

        for (User user : userList) {
            if (user.getId() == 2) {
                user.setName("miao");
            }
        }
        System.out.println(userList);

    }


    @Test
    public void test3() {
        List<Integer> listA = new ArrayList<>();
        List<Integer> listB = new ArrayList<>();

        listA.add(1);
        listA.add(2);
        listA.add(3);
        listA.add(4);
        listA.add(5);
        listA.add(6);

        listB.add(2);
        listB.add(3);
        listB.add(5);

        // boolean retainAll(collector) 将存在于A但不存在于B的元素移除，
        // 就是将A中除了2，3，5之外的元素移除
        // 若result=false,则listA与listB元素相同
        /*boolean result = listA.retainAll(listB);
        System.out.println(result);
        listA.forEach(System.out::println);*/

        // boolean removeAll(collector) 将存在A并同时存在于B的元素移除
        boolean result = listA.removeAll(listB);
        System.out.println(result);
        listA.forEach(System.out::println);

    }

    @Test
    public void test4() {
        List<User> userList = new ArrayList<>();
        userList.add(new User(5, "tom"));
        userList.add(new User(2, "lisi"));
        userList.add(new User(1, "zhangsan"));
        userList.add(new User(4, "zhaoliu"));
        userList.add(new User(3, "wangwu"));
        // 根据id升序排序前
        System.out.println("======根据id升序排序前：======");
        userList.forEach(System.out::println);

        // 根据id升序排序后
        // 方式1
        /*Comparator<User> comparator = new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getId().compareTo(o2.getId());
            }
        };*/
        // 方式2：
        /*Comparator<User> comparator = (o1, o2) -> o1.getId().compareTo(o2.getId());*/

        // 方式3：
        Comparator<User> comparator = Comparator.comparing(User::getId);
        userList.sort(comparator);

        System.out.println("======根据id升序排序后：======");
        userList.forEach(System.out::println);

        // 反转后
        System.out.println("======id反转后：======");
        Collections.reverse(userList);
        userList.forEach(System.out::println);

    }

    @Test
    public void test5() {
        List<Person> personList = Arrays.asList(
                new Person().setId(1).setAge(18).setType("student").setName("user - 1").setGender("male"),
                new Person().setId(2).setAge(20).setType("student").setName("user - 2").setGender("male"),
                new Person().setId(3).setAge(18).setType("student").setName("user - 3").setGender("male"),
                new Person().setId(4).setAge(18).setType("student").setName("user - 4").setGender("male"),
                new Person().setId(5).setAge(35).setType("teacher").setName("user - 5").setGender("male"),
                new Person().setId(6).setAge(35).setType("teacher").setName("user - 6").setGender("male"),
                new Person().setId(7).setAge(20).setType("student").setName("uaer - 7").setGender("male"),
                new Person().setId(8).setAge(20).setType("student").setName("uaer - 8").setGender("female"),
                new Person().setId(9).setAge(20).setType("student").setName("uaer - 9").setGender("female"),
                new Person().setId(10).setAge(20).setType("student").setName("uaer - 10").setGender("female")
        );

        Map<String, List<Person>> groupingMap = personList.stream()
                .collect(Collectors.groupingBy(Person::getGender));

        Map<String, List<Person>> groupingMap2 = personList.stream()
                .collect(Collectors.groupingBy(person -> person.getAge() + person.getName()));
        System.out.println();
    }

     @Test
    public void test6() {
        List<User> userList = new ArrayList<>();
        userList.add(new User(5, "tom"));
        userList.add(new User(2, "lisi"));
        userList.add(new User(1, "zhangsan"));
        userList.add(new User(4, "zhaoliu"));
        userList.add(new User(3, "wangwu"));
 
        Map<Integer, User> groupedMap = userList.stream()
            .collect(Collectors.toMap(
                User::getId, // 分组的键
                user -> user, // 分组的值
                (existing, newUser) -> newUser // 如果键相同，则新值覆盖旧值
            ));
 
        groupedMap.forEach((id, item) -> System.out.println(id + " => " + item.getValue()));
    }

}


